package com.api.common.utils;

import com.api.common.config.ApiConfig;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {

    private static final String SECRET = "AES";
    static String type = "AES";
    static String mode = "CBC"; //mode ECB CBC CFB OFB CTR
    static String pad = "PKCS5Padding";

    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String keyType = "utf8";
//    private static String iv = "e10adc3949ba59ab";
    private static String ivType = "utf8";
    private static boolean isBase64 = true;          //结果输出编码 true: Base64, false: Hex

    public static String aes256ECBPkcs7PaddingEncrypt(String data) throws Exception {
        String key = ApiConfig.getLoginKey();
        String iv = ApiConfig.getOffsetVal();
//        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//        byte[] keyBytes = (keyType == "Hex" ? Hex.decodeHex(key) : keyType == "Base64" ? Base64.decodeBase64(key) : key.getBytes());
//        SecretKeySpec secret = new SecretKeySpec(keyBytes, SECRET);
//        if (mode.equals("ECB")) {
//            cipher.init(Cipher.ENCRYPT_MODE, secret);
//        } else {
//            byte[] ivBytes = (keyType == "Hex" ? Hex.decodeHex(iv) : keyType == "Base64" ? Base64.decodeBase64(iv) : iv.getBytes());
//            cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivBytes));
//        }
//        byte[] bytes = cipher.doFinal(data.getBytes());
//        return isBase64 ? Base64.encodeBase64String(bytes) : Hex.encodeHexString(bytes);


        Cipher cipher = Cipher.getInstance(type + "/" + mode + "/" + pad);
        byte[] keyBytes = (keyType == "Hex" ? Hex.decodeHex(key) : keyType == "Base64" ? Base64.decodeBase64(key) : key.getBytes());
        SecretKeySpec secret = new SecretKeySpec(keyBytes, SECRET);
        if(mode.equals("ECB")) {
            cipher.init(Cipher.ENCRYPT_MODE, secret);
        }else {
            byte[] ivBytes = (ivType == "Hex" ? Hex.decodeHex(key) : ivType == "Base64" ? Base64.decodeBase64(key) : iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivBytes));
        }
        byte[] bytes = cipher.doFinal(data.getBytes());
        return isBase64 ? Base64.encodeBase64String(bytes) : Hex.encodeHexString(bytes);
    }
    /**
     * AES解密ECB模式PKCS7Padding填充方式
     *
     * @param data 字符串
     * @return 解密字符串
     * @throws Exception 异常信息
     */
    public static String aes256ECBPkcs7PaddingDecrypt(String data) throws Exception {
        String iv = ApiConfig.getOffsetVal();
        String key = ApiConfig.getLoginKey();
        return aes256ECBPkcs7PaddingDecrypt(data,iv,key);
    }
    public static String aes256ECBPkcs7PaddingDecrypt(String data ,String iv ,String key) throws Exception {
        System.out.println("ApiConfig.getLoginKey():" + key + ";iv:"+iv);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        byte[] keyBytes = (keyType == "Hex" ? Hex.decodeHex(key) : keyType == "Base64" ? Base64.decodeBase64(key) : key.getBytes());
        SecretKeySpec secret = new SecretKeySpec(keyBytes, SECRET);
        if(mode.equals("ECB")) {
            cipher.init(Cipher.DECRYPT_MODE, secret);
        }else {
            byte[] ivBytes = (ivType == "Hex" ? Hex.decodeHex(key) : ivType == "Base64" ? Base64.decodeBase64(key) : iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));
        }
        byte[] decrypt = cipher.doFinal(isBase64 ? Base64.decodeBase64(data) : Hex.decodeHex(data));
        return new String(decrypt);
    }

    public static String loginDecrypt(String data) throws Exception {
        String iv = "e10adc3949ba59ab";
        String key = "db8ba76ba7862f91";
        return aes256ECBPkcs7PaddingDecrypt(data,iv,key);
    }
}
